package com.naiterui.ehp.bs.esign.controller.inner;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.itextpdf.text.DocumentException;
import com.naiterui.ehp.bp.bo.ca.EmrSignBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.esign.exception.ExceptionCodes;
import com.naiterui.ehp.bs.esign.service.IEmrService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author guoyongxiang
 * @date 2019/9/10 20:48
 * @since 1.0.0
 */
@Slf4j
@RequestMapping("esign/inner/emr")
@RestController
public class EsignInnerEmrController {

    @Autowired
    private IEmrService emrPdfService;

    /**
     * <pre>
     *     生成病例pdf并获取电子签名unionId
     * </pre>
     *
     * @param emrId
     *
     * @return
     *
     * @throws IOException
     * @throws DocumentException
     */
    @PostMapping("presign")
    public ResponseVO<EmrSignBO> unionId(@RequestParam String emrId, @RequestParam Long inquirerId,
        @RequestParam Integer requestFrom) throws BusinessException {
        log.info("获取病例unionId开始 emrId:{}, inquirerId:{}, requestFrom:{}", emrId, inquirerId, requestFrom);
        EmrSignBO bo = emrPdfService.getUnionId(emrId, inquirerId, requestFrom);
        log.info("获取病例unionId成功 signBO:{}=", bo.toString());
        return PublicService.returnResponseVO(bo);
    }

    /**
     * <pre>
     *     服务端签名:e签宝签名由服务端发起
     * </pre>
     *
     * @param emrId
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("sign")
    public ResponseVO<Void> sign(@RequestParam String emrId, String pin, Integer requestFrom) throws BusinessException {
        log.info("病例签名开始 emrId:{}, pin: {}, requestFrom:{}", emrId, pin, requestFrom);
        emrPdfService.sign(emrId, pin, requestFrom);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }
}
